var userOpt = function(){
	//缓存
	var docObject = null;
	var userKey = null;
	var method = null;
	
	return{
		updatePhoto:function(elementid){
			var tagsInput = $("#tagsInput").val();
			var index = tagsInput.lastIndexOf(".");
			var typeTag = tagsInput.substr(index);
			if('.jpg' == typeTag || ".png"==typeTag || ".gif"==typeTag ||
					".bmp"==typeTag ){	
				$.ajaxFileUpload({
					type:'post',
					url: weburl + 'profile/uploadPhoto?userKey='+userKey,//用于文件上传的服务器端请求地址
			      	secureuri:false,//一般设置为false
			      	fileElementId:'tagsInput',//文件上传空间的id属性 
			     	dataType:'json',//返回值类型 一般设置为json
			     	data:{'method': method,'userKey':userKey},
			     	success:function (data, status){
			     		if("yes" == data.result){
			     			$("#"+elementid +" img:first").attr('src',data.path);
			     		}else{
			     			alert("上传失败");
			     		}
			     		$("span").remove('#updateValue');
						$("span").remove('#updateButton');
			      	},
			      	error:function (data, status, e){
			      		alert("error");
			        }
			    });
			} else {
				alert("只支持jpg、png、gif、bmp格式图片上传！");
			}
		},
		updateName:function(elementid){
			var tagsInput = $("#tagsInput").val();
			$.ajax({
				'url': weburl + "profile/userOpt",
				'data': {'method': method, 'updateValue': tagsInput,'userKey':userKey},
				'success': function( data ){
					if(data == "no"){
						alert("修改失败！");
					}else{
						//卸载变更
						$("#"+elementid +" span:first").text(tagsInput);
					}
					$("span").remove('#updateValue');
					$("span").remove('#updateButton');
				}
			});
		},
		updatePassword:function(elementid){
			var tagsInput = $("#tagsInput").val();
			$.ajax({
				'url': weburl + "profile/userOpt",
				'data': {'method': method, 'updateValue': tagsInput,'userKey':userKey},
				'success': function( data ){
					//卸载变更
					if(data == "no"){
						alert("修改失败！");
					}
					
					$("span").remove('#updateValue');
					$("span").remove('#updateButton');
				}
			});
		},
		modifyName:function(elementid,userid){
			userKey = userid;
			//卸载变更
			$("span").remove('#updateValue');
			$("span").remove('#updateButton');
			//添加新输入框和按钮
			docObject =  $("#"+elementid);
			
			if(docObject != null){
				originHtml = docObject.html();
				method = "updateUsername";
				var tagsInput = $("#"+elementid +" span:first").text();
				$("<span id='updateValue'><input id='tagsInput' type='text' name='tagsInput' value='"+$.trim(tagsInput)+"' size='20' /></span>").appendTo("#"+elementid +" span:last");
				$("<span id='updateButton'><input type='button' value='保存' onclick='userOpt.updateName(\""+elementid+"\")'/></span>").appendTo("#"+elementid +" span:last");
			}
			
		},
		modifyPassword:function(elementid,userid){
			userKey = userid;
			//卸载变更
			$("span").remove('#updateValue');
			$("span").remove('#updateButton');
			//添加新输入框和按钮
			docObject =  $("#"+elementid);
			
			if(docObject != null){
				method = "updatePassword";
				$("<span id='updateValue'><input id='tagsInput' type='password' name='tagsInput' size='20' /></span>").appendTo("#"+elementid +" span:last");
				$("<span id='updateButton'><input type='button' value='保存' onclick='userOpt.updatePassword(\""+elementid+"\")'/></span>").appendTo("#"+elementid +" span:last");
			}
		},
		modifyPhoto:function(elementid,userid){
			userKey = userid;
			//卸载变更
			$("span").remove('#updateValue');
			$("span").remove('#updateButton');
			//添加新输入框和按钮
			docObject =  $("#"+elementid);
			if(docObject != null){
				method = "updatePhoto";
				$("<span id='updateValue' style='float: left;'><input id='tagsInput' type='file' name='tagsInput' size='20' /></span>").appendTo("#"+elementid +" a:last");
				$("<span id='updateButton' style='float: left;'><input type='button' value='上传' onclick='userOpt.updatePhoto(\""+elementid+"\")'/></span>").appendTo("#"+elementid +" span:last");
			}
		},
		watch:function(userid,watchUserid,watch){
			var method = "watchuser";
			$.ajax({
				'url': "ajaxuseropt.php",
				'data': {'method': method, 'userid': userid,
					'watchUserid':watchUserid,'watch':watch},
				'success': function( data ){
					eval(" var obj = "+data);
					if("yes" == obj.result){
						if(watch == 1){
							$("#watchUsers").attr('href',"javascript:userOpt.watch('"+userid+"','"+watchUserid+"','0');");
							$("#watchUsers").text("关注");
							
						}else{
							$("#watchUsers").attr('href',"javascript:userOpt.watch('"+userid+"','"+watchUserid+"','1');");
							$("#watchUsers").text("取消关注");
						}
					}
				}
			});
		},
		
		watchuserlist:function(userid,watchUserid,site){
			var method = "watchuserlist";
			$.ajax({
				'url': "ajaxuseropt.php",
				'data': {'method': method, 'userid': userid,
					'watchUserid':watchUserid},
				'success': function( data ){
					eval(" var obj = "+data);
					var html = "";
					for(var watcher in  obj){
						html += "<li><span> <a href='space.php?uid="+obj[watcher].userid+"'> "+
							"<img src='"+obj[watcher].photo+"'  />粉丝："+obj[watcher].wcount+"</a></span></li>";
					}
					$('#watchuserlist').append(html);
				}
			});
		}
	}
}();